package com.mabang.medata.serviceImpl;

import java.text.NumberFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.jws.WebService;
import javax.xml.ws.BindingType;
import javax.xml.ws.soap.SOAPBinding;

import org.apache.cxf.common.util.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.mabang.medata.commons.ConstantStatus;
import com.mabang.medata.commons.CoverInsuranceVo;
import com.mabang.medata.dao.CoverInsuranceMapper;
import com.mabang.medata.dao.RiskCoefficientMapper;
import com.mabang.medata.model.Quote;
import com.mabang.medata.procotol.QuoteProcotol;
import com.mabang.medata.service.QuoteService;
import com.mabang.medata.util.Utils;
import com.mabang.medata.util.XmlUtil;

@BindingType(value = SOAPBinding.SOAP12HTTP_BINDING)
@Component
@WebService(name = "QuoteService", targetNamespace = "http://service.medata.mabang.com/")
public class QuoteServiceImpl implements QuoteService {
	@Autowired
	private CoverInsuranceMapper coverInsuranceMapper;
	@Autowired
	private RiskCoefficientMapper riskCoefficientMapper;
	NumberFormat percent = NumberFormat.getPercentInstance();
	Pattern pat = null;
	private String reg = "[\u4e00-\u9fa5]";

	public QuoteServiceImpl() {
		percent.setMaximumFractionDigits(4);
		pat = Pattern.compile(reg);
	}
	Logger logger = Logger.getLogger(QuoteServiceImpl.class);

	@Override
	public String getQuoteNo(String quote) {
		XmlUtil xmlUtil;
		String responseXml = null;
		String quoteNo = null,message=ConstantStatus.MESSAGE_ERROR,status = ConstantStatus.SERVICE_UNAVAILABLE;;
		try {
			xmlUtil = new XmlUtil(quote);
			quoteNo = xmlUtil
					.getSingleChildElementValue("proposal", "quote-no");
			if (StringUtils.isEmpty(quoteNo)) {
				status = ConstantStatus.NO_CONTENT;
				message = "传入的报价单为空";
			} else {
				logger.debug("解析后的报价单号：" + quoteNo);
				CoverInsuranceVo coverInsurance = null;
				try {
					coverInsurance = coverInsuranceMapper
							.selectByCoverInsuranceCode(quoteNo);
				} catch (Exception e) {
					status = ConstantStatus.SERVICE_UNAVAILABLE;
					message = "系统异常";
				}
				if (coverInsurance != null) {
					if (coverInsurance.getCoverInsuranceStatus().equals("006002")) {
						// 报价单无效
						status = ConstantStatus.QUOTE_UNVALID;
						 if(coverInsurance.getChangeType() != null){
	                            if(coverInsurance.getChangeType().intValue() == 1){
	                                message = ConstantStatus.MESSAGE_AUTO;
	                            }else{
	                                message = ConstantStatus.MESSAGE_DO;
	                            }
	                        }else{
	                            message = ConstantStatus.MESSAGE_DO;
	                        }
					} else {
						String termCoefficient = coverInsurance
								.getTermCoefficientCodeName();
						String termCoefficientCode = coverInsurance
								.getTermCoefficientCode3Name();
						String value = Utils.replaceBlank(termCoefficient).split("-")[0];
						termCoefficient = value.replace("万", "0000")
								.replace("千万", "0000000")
								.replace("亿", "00000000");
						Matcher matterm = pat.matcher(termCoefficient);
						termCoefficient = matterm.replaceAll("");
						String[] result = Utils.replaceBlank(
								termCoefficientCode).split("-");
						String valueCode=null;
						valueCode=result.length>1?result[0]:result[0].replace("%", "");	
						Matcher mat = pat.matcher(valueCode);
						valueCode = mat.replaceAll("");
						Quote quoteBean = new Quote(
								coverInsurance.getCoverInsuranceCode(),
								coverInsurance.getCustomerName(),
								coverInsurance.getTradeCode(),
								percent.format(coverInsurance.getCoverRate()),
								termCoefficient, valueCode,
								Utils.dateToString(new Date()),
								ConstantStatus.OK,ConstantStatus.MESSAGE_OK);
						responseXml = QuoteProcotol.responseQuoteNo(quoteBean);
						return responseXml;
					}
				} else {
					status = ConstantStatus.NOT_FOUND;
					message = "找不到报价单信息";
				}
			}
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		Quote quoteBean = new Quote(quoteNo, "", "", "", "", "",
				Utils.dateToString(new Date()), status,message);
		responseXml = QuoteProcotol.responseQuoteNo(quoteBean);
		return responseXml;
	}
}
